package com.yanqu.road.server.cmd.activity.sonhai;

import com.yanqu.road.entity.activity.sonhai.param.SonHaiChooseResp;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.pb.SonHaiActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_SON_HAI_RECEIVE_CHOOSE_REWARD, desc = "山海伏兽-2选1，选择奖励")
public class CrossSonHaiReceiveChooseRewardCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        SonHaiProto.SonHaiReceiveChooseRewardReqMsg reqMsg = SonHaiProto.SonHaiReceiveChooseRewardReqMsg.parseFrom(packet.getBytes());
        SonHaiProto.SonHaiReceiveChooseRewardRespMsg.Builder respMsg = SonHaiProto.SonHaiReceiveChooseRewardRespMsg.newBuilder();
        CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_SON_HAI_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_RECEIVE_CHOOSE_REWARD, respMsg));
            return;
        }
        //2选1，选择奖励
        try {
            //2选1，选择奖励
            SonHaiChooseResp chooseResp = activity.receiveChooseEventReward(packet.getPlayerId(), reqMsg.getX(), reqMsg.getY(), reqMsg.getRightOrWrong());
            respMsg.setRet(0);
            respMsg.setReward(chooseResp.getReward());
            respMsg.setActivityId(reqMsg.getActivityId());
            respMsg.setChooseConfigId(chooseResp.getChooseConfigId());
            respMsg.setIndex(chooseResp.getIndex());
            respMsg.setChooseTrue(chooseResp.isChooseTrue());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_SON_HAI_RECEIVE_CHOOSE_REWARD_FROM_CROSS, respMsg));
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_SON_HAI_RECEIVE_CHOOSE_REWARD, respMsg));
        }
    }

    @Override
    public void sendData() {

    }
}
